package com.ddstation.patient.service;

import java.util.List;

import javax.inject.Inject;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ddstation.common.util.AgeUtil;
import com.ddstation.patient.model.UserModel;
import com.ddstation.user.health_file.entity.DdRecordBaseInfoEntity;
import com.ddstation.user.health_file.mapper.DdRecordBaseInfoMapper;

@Service
public class UserInfoServiceImpl implements UserInfoServiceItf {

	/**
	 * 用户可以没有对应的关注数据（public follow） 用户PC版健康管理系统
	 * @param uid
	 * @param childId
	 * @return
	 */
	@Override
	@Transactional(readOnly=true)
	public UserModel fetchBaseUserInfo(String uid, int childId) {
		// TODO Auto-generated method stub
		DdRecordBaseInfoEntity condition = new DdRecordBaseInfoEntity();
		condition.setUid(uid);
		condition.setChildId(childId);
		List<DdRecordBaseInfoEntity> users = ddRecordBaseInfoMapper.selectBaseInfo(condition);
		if (users != null && users.size() > 0) {
			UserModel user = new UserModel();
				
			DdRecordBaseInfoEntity userInfo = users.get(0);
			user.setName(userInfo.getName());
			user.setGender(userInfo.getGender());
			user.setFimalyAddrAllTxt(userInfo.getFimalyAddr());
			user.setFimalyAddr(userInfo.getFimalyAddr());
			user.setPhone(userInfo.getPhone());
			user.setChildId(userInfo.getChildId());
			user.setBirthday(userInfo.getBirthday());
			
			if (user.getChildId() == 0) {
				user.setRelationShip("自己");
			}
			
			// 计算年龄
			if (user.getBirthday() != null) {
				user.setAge(AgeUtil.getAge(user.getBirthday()));
			}
			return user;
		} else {
			return null;
		}
	}
	
	@Inject private DdRecordBaseInfoMapper ddRecordBaseInfoMapper;
}
